在前兩篇提及的Vue Style Guide是比較重要的項目,而這篇會所建議的是依個人習慣做的推薦,依照自己喜好即可~
E.G.
props: {
value: {
type: String,
required: true
},
focused: {
type: Boolean,
default: false
},
label: String,
icon: String
},
computed: {
formattedValue: function () {
// ...
},
inputClasses: function () {
// ...
}
}
// 即使這樣,Component options沒有換行也沒關係,還是可以很清楚的閱讀程式碼
props: {
value: {
type: String,
required: true
},
focused: {
type: Boolean,
default: false
},
label: String,
icon: String
},
computed: {
formattedValue: function () {
// ...
},
inputClasses: function () {
// ...
}
}
script
、template
、style
標籤保持一致性style
E.G.
<!-- ComponentA.vue -->
<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>
<!-- ComponentB.vue -->
<script>/* ... */</script>
<template>...</template>
<style>/* ... */</style>
Vue官網建議的Component/instance options order、Element attribute order,不在這裡多述。
(官網已經寫得很清楚了QQ)
<div>
元素)E.G.
<div
v-if="error"
key="search-status"
>
Error: {{ error }}
</div>
<div
v-else
key="search-results"
>
{{ results }}
</div>
Bad Smell
E.G.
<template>
<button>X</button>
</template>
<style scoped>
button {
background-color: red;
}
</style>
Good Smell
E.G.
<template>
<button class="btn btn-close">X</button>
</template>
<style scoped>
.btn-close {
background-color: red;
}
</style>
E.G.
use.component('TodoItem', {
props: {
todo: {
type: Object,
required: true
}
},
template: `
<input
:value="todo.text"
@input="$emit('input', $event.target.value)"
>
`
})
Vue.component('TodoItem', {
props: {
todo: {
type: Object,
required: true
}
},
template: `
<span>
{{ todo.text }}
<button @click="$emit('delete')">
X
</button>
</span>
`
})
Vue Style Guide到這裡告一段落,中場休息結束! 下一篇即將回歸Vue API - nextTick